package com.change.study.mongodb.service;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import com.change.study.data.core.Pagination;
import com.change.study.mongodb.domain.Back;
import com.change.study.mongodb.domain.Maopao.Comment;
import com.change.study.mongodb.domain.Maopao.Like_user;
import com.change.study.mongodb.domain.MaopaoObject;
import com.change.study.mongodb.domain.MaopaoObjectBack;
import com.google.gson.Gson;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.WriteResult;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.util.JSON;

public class MaopaoServiceImpl implements MaopaoService {

	MongoTemplate mongoTemplate;

	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}

	@Override
	public Back Add(MaopaoObject postAction) {
		Back back = new Back();
		try {
			mongoTemplate.insert(postAction);

		} catch (Exception e) {
			e.printStackTrace();
			back.setMessage("erro");
		}
		return back;
		// TODO Auto-generated method stub

	}

	@Override
	public Back delete(String id) {
		Back back = new Back();
		WriteResult r = mongoTemplate.remove(
				new Query(Criteria.where("id").is(id)), MaopaoObject.class);
		System.out.println(r);
		return back;
	}

	@Override
	public List<MaopaoObject> pape(int curpage, int pagesize) {
		int skip = 0;
		if (curpage > 1) {
			skip = (curpage - 1) * pagesize;
		}
		Query query = new Query();
		query.limit(pagesize);
		query.skip(skip);
		Sort arg0 = new Sort(Direction.DESC, "sorttime");
		query.with(arg0);
		return mongoTemplate.find(query, MaopaoObject.class);
	}

	@Override
	public Back like(String actionid, Like_user postLike) {
		Back back = new Back();

		MaopaoObject action = findById(actionid);
		action.like_users.add(postLike);
		mongoTemplate.updateFirst(new Query(Criteria.where("id").is(actionid)),
				Update.update("likes", action.like_users), MaopaoObject.class);
		return back;
	}

	@Override
	public Back unlike(String actionid, Like_user postLike) {
		MaopaoObject action = findById(actionid);
		action.like_users.remove(postLike);
		mongoTemplate.updateFirst(new Query(Criteria.where("id").is(actionid)),
				Update.update("likes", action.like_users), MaopaoObject.class);
		return null;
	}

	@Override
	public Back comment(String actionid, Comment comment) {
		Back back = new Back();

		MaopaoObject action = findById(actionid);
		action.comment_list.add(comment);
		mongoTemplate.updateFirst(new Query(Criteria.where("id").is(actionid)),
				Update.update("comments", action.comment_list),
				MaopaoObject.class);
		return back;
	}

	@Override
	public Back uncomment(String actionid, Comment comment) {

		Back back = new Back();

		MaopaoObject action = findById(actionid);
		action.comment_list.remove(comment);
		mongoTemplate.updateFirst(new Query(Criteria.where("id").is(actionid)),
				Update.update("comments", action.comment_list),
				MaopaoObject.class);
		return back;
	}

	@Override
	public MaopaoObject findById(String id) {
		return mongoTemplate.findOne(new Query(Criteria.where("id").is(id)),
				MaopaoObject.class);
	}

	@Override
	public MaopaoObjectBack find(int curpage, int pagesize) {
		MaopaoObjectBack resutl = new MaopaoObjectBack();
		List<MaopaoObject> postActions = pape(curpage, pagesize);
		resutl.setActions(postActions);
		return resutl;
	}

	@Override
	public Pagination findByUser(String userid, int curpage, int pagesize) {
		int skip = 0;
		if (curpage > 1) {
			skip = (curpage - 1) * pagesize;
		}
		MongoClient mongoClient1 = new MongoClient();
		MongoDatabase db = mongoClient1.getDatabase("study");
		MongoCollection colloe = db.getCollection("MaopaoObject");
		FindIterable cursor = colloe.find();
		Document filter = new Document();// BasicDBObject("user.username",
											// "ada");
		filter.put("user.id", userid);
		filter.put("type", "link");
		int size = filter.size();
		Pagination pagination = new Pagination<MaopaoObject>(curpage, pagesize,
				size);
		cursor = cursor.filter(filter);
		cursor.limit(pagesize);
		cursor.skip(skip);
		MongoCursor cc = cursor.iterator();
		List<MaopaoObject> ps = new ArrayList<MaopaoObject>();
		while (cc.hasNext()) {
			Document doc = (Document) cc.next();

			DBObject docx = (DBObject) JSON.parse(doc.toJson());
			;
			// MaopaoObject action=new MaopaoObject();
			// action.setId(doc.getString("_id"));
			// action.setType(doc.getString("type"));
			// action.setMessage(doc.getString("message"));
			// PostLink postLink=new PostLink();
			// postLink.setUrl(doc.get("postLink",
			// Document.class).getString("url"));
			// action.setPostLink(postLink);
			// MaopaoObject a = Converter.toObject(MaopaoObject.class, docx);
			// ps.add(a);
		}
		pagination.setList(ps);
		return pagination;
	}

	@Override
	public MaopaoObjectBack findByName(String userid, int curpage, int pagesize) {
		int skip = 0;
		if (curpage > 1) {
			skip = (curpage - 1) * pagesize;
		}

		MaopaoObjectBack pagination = null;

		Query query = new Query();
		query.addCriteria(Criteria.where("owner.name").is(userid));
		List<MaopaoObject> postActions = mongoTemplate.find(query,
				MaopaoObject.class);
		int totalCount=(int) mongoTemplate.count(query, MaopaoObject.class);
		Pagination<MaopaoObject> pagination1 = new Pagination<MaopaoObject>(curpage, pagesize, totalCount);
		pagination1.setList(postActions);
		pagination=new MaopaoObjectBack();
		pagination.setActions(postActions);
		return pagination;
	}

}
